
<template>
  <a-spin :spinning="confirmLoading">
    
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      
      <a-row>
        <a-col :span="12" v-if="!disabled">  
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="AI优化参数">
            <j-search-select 
              @click="AIButton=false"
              placeholder="请选择产品名称" 
              dict="camellia_mes_product,name,id" 
              :disabled="disabled" 
              v-model:value="formData.name"
              />
          </a-form-item>     
        </a-col>

        <a-col :span="12">
            <a-button style="position: absolute;left:20px;" type="primary" shape="circle" :disabled="AIButton" @click="optimize">AI</a-button>
        </a-col>
        <a-col>
          <a-row>
            <a-col :span="12">
              <a-form-item :labelCol="{ style: 'width: 160px' }" label="工艺名称" v-bind="validateInfos.craftName">
                <a-input v-model:value="formData.craftName" placeholder="请输入工艺名称" :disabled="disabled"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item :labelCol="{ style: 'width: 160px' }" label="萃取次数设置" v-bind="validateInfos.leachTs">
                <a-input-number v-model:value="formData.leachTs" placeholder="请输入萃取次数设置" style="width: 100%" :disabled="disabled" />
              </a-form-item>
            </a-col>
          </a-row>
        </a-col>
        <a-col>
          
          <a-row>
          <a-col :span="12">
            <a-form-item :labelCol="{ style: 'width: 160px' }" label="计量设定时间(s)" v-bind="validateInfos.meterSt">
              <a-input-number v-model:value="formData.meterSt" placeholder="请输入计量设定时间(s)" style="width: 100%" :disabled="disabled" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="{ style: 'width: 160px' }" label="计量延迟时间(s)" v-bind="validateInfos.meterDt">
              <a-input-number v-model:value="formData.meterDt" placeholder="请输入计量延迟时间(s)" style="width: 100%" :disabled="disabled" />
            </a-form-item>
          </a-col>
          </a-row>
        </a-col>
        <a-col>
          <a-row>
          <a-col :span="12">
            <a-form-item :labelCol="{ style: 'width: 160px' }" label="萃取进液设定时间(s)" v-bind="validateInfos.intakeSt">
              <a-input-number v-model:value="formData.intakeSt" placeholder="请输入萃取进液设定时间(s)" style="width: 100%" :disabled="disabled" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="{ style: 'width: 160px' }" label="萃取出液设定时间(s)" v-bind="validateInfos.leachSt">
              <a-input-number v-model:value="formData.leachSt" placeholder="请输入萃取出液设定时间(s)" style="width: 100%" :disabled="disabled" />
            </a-form-item>
          </a-col>
          </a-row>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }" label="萃取出液延迟蒸发时间(s)" v-bind="validateInfos.vapDt">
            <a-input-number v-model:value="formData.vapDt" placeholder="请输入萃取出液延迟蒸发时间(s)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }" label="浸泡设定时间(s)" v-bind="validateInfos.dipinSt">
            <a-input-number v-model:value="formData.dipinSt" placeholder="请输入浸泡设定时间(s)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }" label="延迟时间设置(s)" v-bind="validateInfos.delaySt">
            <a-input-number v-model:value="formData.delaySt" placeholder="请输入延迟时间设置(s)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }" label="真空计量时间设置(s)" v-bind="validateInfos.vacuoMst">
            <a-input-number v-model:value="formData.vacuoMst" placeholder="请输入真空计量时间设置(s)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }" label="真空压力设置(kPa)" v-bind="validateInfos.vacuoPre">
            <a-input-number v-model:value="formData.vacuoPre" placeholder="请输入真空压力设置(kPa)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="压机压力设置(kPa)" v-bind="validateInfos.comprPre">
            <a-input-number v-model:value="formData.comprPre" placeholder="请输入压机压力设置(kPa)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="热水温度设置(℃)" v-bind="validateInfos.hwaterTem">
            <a-input-number v-model:value="formData.hwaterTem" placeholder="请输入热水温度设置(℃)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }" label="热水温度预减1(℃)" v-bind="validateInfos.hwaterDn1">
            <a-input-number v-model:value="formData.hwaterDn1" placeholder="请输入热水温度预减1(℃)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }" label="热水温度预减2(℃)" v-bind="validateInfos.hwaterDn2">
            <a-input-number v-model:value="formData.hwaterDn2" placeholder="请输入热水温度预减2(℃)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="萃取温度设置(℃)" v-bind="validateInfos.leachTem">
            <a-input-number v-model:value="formData.leachTem" placeholder="请输入萃取温度设置(℃)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="蒸发温度设置(℃)" v-bind="validateInfos.vapTem">
            <a-input-number v-model:value="formData.vapTem" placeholder="请输入蒸发温度设置(℃)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="空气罐压力设置(kPa)" v-bind="validateInfos.airPre">
            <a-input-number v-model:value="formData.airPre" placeholder="请输入空气罐压力设置(kPa)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="萃取器脱溶时间(s)" v-bind="validateInfos.leachDst">
            <a-input-number v-model:value="formData.leachDst" placeholder="请输入萃取器脱溶时间(s)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="过滤器脱溶时间设置(s)" v-bind="validateInfos.filterDst">
            <a-input-number v-model:value="formData.filterDst" placeholder="请输入过滤器脱溶时间设置(s)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="真空脱溶时间设置(s)" v-bind="validateInfos.vacuoDst">
            <a-input-number v-model:value="formData.vacuoDst" placeholder="请输入真空脱溶时间设置(s)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="{ style: 'width: 160px' }"  label="冷水温度设置(℃)" v-bind="validateInfos.cwaterSt">
            <a-input-number v-model:value="formData.cwaterSt" placeholder="请输入冷水温度设置(℃)" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
import { ref, reactive, defineExpose, nextTick, defineProps, computed } from 'vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { getValueType } from '/@/utils';
import { saveOrUpdate } from '../CamelliaMesProcessCraft.api';
import { Form } from 'ant-design-vue';
import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
import { message } from 'ant-design-vue';

import { defHttp } from '/@/utils/http/axios';

const props = defineProps({
  formDisabled: { type: Boolean, default: false },
  formData: { type: Object, default: () => {} },
  formBpm: { type: Boolean, default: true },
});
const formRef = ref();
const useForm = Form.useForm;
const emit = defineEmits(['register', 'ok']);
const formData = reactive<Record<string, any>>({
  id: '',
  craftName: '',
  leachTs: undefined,
  meterSt: undefined,
  meterDt: undefined,
  intakeSt: undefined,
  leachSt: undefined,
  vapDt: undefined,
  dipinSt: undefined,
  delaySt: undefined,
  vacuoMst: undefined,
  vacuoPre: undefined,
  comprPre: undefined,
  hwaterTem: undefined,
  hwaterDn1: undefined,
  hwaterDn2: undefined,
  leachTem: undefined,
  vapTem: undefined,
  airPre: undefined,
  leachDst: undefined,
  filterDst: undefined,
  vacuoDst: undefined,
  cwaterSt: undefined,
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
const confirmLoading = ref<boolean>(false);
const AIButton = ref<boolean>(true);
//表单验证
const validatorRules = {
  craftName: [{ required: true, message: '请输入工艺名称!' }],
};
const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

// 表单禁用
const disabled = computed(() => {
  if (props.formBpm === true) {
    if (props.formData.disabled === false) {
      return false;
    } else {
      return true;
    }
  }
  return props.formDisabled;
});

//AI优化
const key = 'updatable'
function optimize() {
  // message.loading({
  //   content: 'AI优化中...',
  //   key,
  // });

    
    setForm();
  
}
function setForm(){
  console.log(formData.name)
  // formData.leachTs = 3;
  // formData.meterSt = 45;
  // formData.meterDt = 50;
  // formData.intakeSt = 25;
  // formData.leachSt = 30;
  // formData.vapDt = 60;
  // formData.dipinSt = 30;
  // formData.delaySt = 45;
  // formData.vacuoMst = 50;
  // formData.vacuoPre = 20;
  // formData.comprPre = 100;
  // formData.hwaterTem = 25;
  // formData.hwaterDn1 = 20;
  // formData.hwaterDn2 = 15;
  // formData.leachTem = 40;
  // formData.vapTem = 110;
  // formData.airPre = 90;
  // formData.leachDst = 40;
  // formData.filterDst = 40;
  // formData.vacuoDst = 40;
  // formData.cwaterSt = 10;

    let url = '/mes/camelliaMesExtraTechParams/optimize'
    let params ={id:formData.name}
    defHttp.get({ url, params }).then((respose) => {
    console.log(respose);
    console.log(respose.temp);
    formData.temp = respose.temp
    formData.leachTs = respose.times
    // message.success({
    //   content: '优化完成',
    //   key,
    //   duration: 1,
    // });
    // axios.get("http://localhost:8181/index?name=zhangsan&age=18")
    //             .then(res => {
    //                 console.log(res.data);
    //                 this.stu = res.data;
    //             })
    //             .catch(error => {
    //                 console.log(error);
    //             })

  });
  
}

/**
 * 新增
 */
function add() {
  edit({});
}

/**
 * 编辑
 */
function edit(record) {
  nextTick(() => {
    resetFields();
    //赋值
    Object.assign(formData, record);
  });
}

/**
 * 提交数据
 */
async function submitForm() {
  // 触发表单验证
  await validate();
  confirmLoading.value = true;
  const isUpdate = ref<boolean>(false);
  //时间格式化
  let model = formData;
  if (model.id) {
    isUpdate.value = true;
  }
  //循环数据
  for (let data in model) {
    //如果该数据是数组并且是字符串类型
    if (model[data] instanceof Array) {
      let valueType = getValueType(formRef.value.getProps, data);
      //如果是字符串类型的需要变成以逗号分割的字符串
      if (valueType === 'string') {
        model[data] = model[data].join(',');
      }
    }
  }
  await saveOrUpdate(model, isUpdate.value)
    .then((res) => {
      if (res.success) {
        createMessage.success(res.message);
        emit('ok');
      } else {
        createMessage.warning(res.message);
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

defineExpose({
  add,
  edit,
  submitForm,
  AIButton
});
</script>

<style lang="less" scoped>
::v-deep.ant-modal{
  width: 1000px!important;
}

.antd-modal-form {
  width: 1000px;
  min-height: 500px !important;
  overflow-y: auto;
  padding: 24px 24px 24px 24px;
}
</style>
